package com.campus.server.dao;

import com.campus.common.util.DBUtil;
import com.campus.common.vo.Admin;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class AdminAccountManagementDao {
    // 获取所有书籍
    public List<Admin> listAllAdmins() {
        List<Admin> admins = new ArrayList<>();
        String sql = "SELECT * FROM admin";

        try (Connection conn = DBUtil.getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {

            while (rs.next()) {
                admins.add(new Admin(
                        rs.getString("adminId")
                ));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return admins;
    }


    // 新增学生
    public boolean addAdmin(String username, String password, Admin admin) {
        try (Connection conn = DBUtil.getConnection(); //从DBUtil获取数据库连接
             Statement st = conn.createStatement(); //创建Statement对象用于执行SQL
             ResultSet rs = st.executeQuery("SELECT * FROM admin")) { //执行查询，返回结果集
            //遍历结果集的每一行
            while (rs.next()) {
                if (rs.getString("adminId").equals(username)) {
                    return false;
                }
            }
        } catch (Exception e) { e.printStackTrace(); }

        String sql = "INSERT INTO user1 (username,password,rule) VALUES (?,?,?)";

        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {

            stmt.setString(1, admin.getAdminId());
            stmt.setString(2,"123456");
            stmt.setString(3,"管理员");

            int rowsAffected_1 = stmt.executeUpdate();
            return rowsAffected_1 > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    // 删除学生
    public boolean deleteAdmin(String adminId) {
        String sql_1="DELETE FROM admin WHERE adminId=?";
        String sql_2="DELETE FROM user1 WHERE username=?";

        int rowsAffected_1=0;
        int rowsAffected_2=0;

        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt_1 = conn.prepareStatement(sql_1);
             PreparedStatement stmt_2 = conn.prepareStatement(sql_2);) {

            stmt_1.setString(1, adminId);
            stmt_2.setString(1, adminId);
            rowsAffected_1 = stmt_1.executeUpdate();
            rowsAffected_2 = stmt_2.executeUpdate();
            System.out.println("adminId=" + adminId);
            System.out.println("rowsAffected = " + rowsAffected_1);
            System.out.println(rowsAffected_1>0);

            return rowsAffected_1>0 && rowsAffected_2>0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    // 根据ID查询书籍
    public Admin getAdminById(String adminId) {
        String sql = "SELECT * FROM admin WHERE adminId = ?";

        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {

            stmt.setString(1, adminId);
            ResultSet rs = stmt.executeQuery();

            if (rs.next()) {
                return new Admin(
                        rs.getString("adminId")
                );
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }


    // 综合查询（根据书名和作者模糊查询）
    public List<Admin> searchAdmins(String adminId) {
        List<Admin> students = new ArrayList<>();

        // 构建动态SQL
        StringBuilder sqlBuilder = new StringBuilder("SELECT * FROM admin WHERE 1=1");
        List<Object> params = new ArrayList<>();

        if (adminId != null && !adminId.trim().isEmpty()) {
            sqlBuilder.append(" AND adminId LIKE ?");
            params.add("%" + adminId + "%");
        }

        if (adminId != null && !adminId.trim().isEmpty()) {
            sqlBuilder.append(" AND name LIKE ?");
            params.add("%" + adminId + "%");
        }

        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sqlBuilder.toString())) {

            // 设置参数
            for (int i = 0; i < params.size(); i++) {
                stmt.setObject(i + 1, params.get(i));
            }

            ResultSet rs = stmt.executeQuery();

            while (rs.next()) {
                students.add(new Admin(
                        rs.getString("adminId")
                ));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return students;
    }


}
